Imports System.Data.SqlClient
Public Class cls_unidaddetalle_

    Implements ICloneable

#Region "Variables ..."
    Private int_idunidaddetalle As Integer = 0
    Private int_idunidad As Integer = 0
    Private str_nombre As String = ""
    Private str_simbolo As String = ""
    Private dbl_relacion1 As Double = 0
    Private dbl_relacion2 As Double = 0
    Private int_orden As Integer = 0
    Private bit_primario As Boolean = False

    Private obj_unidad As cls_unidad_

    Private col_Items As Collections.Generic.List(Of cls_unidaddetalle_) = Nothing
#End Region

    Public Overrides Function ToString() As String
        Return nombre
    End Function

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idunidaddetalle
        End Get
    End Property
    Property idunidaddetalle() As Integer
        Get
            idunidaddetalle = int_idunidaddetalle
        End Get
        Set(ByVal value As Integer)
            int_idunidaddetalle = value
        End Set
    End Property
    Property idunidad() As Integer
        Get
            idunidad = int_idunidad
        End Get
        Set(ByVal value As Integer)
            int_idunidad = value
        End Set
    End Property
    Property nombre() As String
        Get
            nombre = str_nombre
        End Get
        Set(ByVal value As String)
            str_nombre = value
        End Set
    End Property
    Property simbolo() As String
        Get
            simbolo = str_simbolo
        End Get
        Set(ByVal value As String)
            str_simbolo = value
        End Set
    End Property
    Property relacion1() As Double
        Get
            relacion1 = dbl_relacion1
        End Get
        Set(ByVal value As Double)
            dbl_relacion1 = value
        End Set
    End Property
    Property relacion2() As Double
        Get
            relacion2 = dbl_relacion2
        End Get
        Set(ByVal value As Double)
            dbl_relacion2 = value
        End Set
    End Property
    Property orden() As Integer
        Get
            orden = int_orden
        End Get
        Set(ByVal value As Integer)
            int_orden = value
        End Set
    End Property
    Property primario As Boolean
        Get
            primario = bit_primario
        End Get
        Set(ByVal value As Boolean)
            bit_primario = value
        End Set
    End Property
    Property unidad() As cls_unidad_
        Get
            If IsNothing(obj_unidad) Then
                obj_unidad = New cls_unidad_
                If Not obj_unidad.Buscar(idunidad) Then
                    obj_unidad = Nothing
                End If
            End If
            unidad = obj_unidad
        End Get
        Set(ByVal value As cls_unidad_)
            obj_unidad = value
        End Set
    End Property
    Property Items() As Collections.Generic.List(Of cls_unidaddetalle_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_unidaddetalle_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_unidaddetalle_)
        While obj_Reader.Read()
            Dim Elemento As New cls_unidaddetalle_
            Elemento.int_idunidaddetalle = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idunidaddetalle")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idunidad"))) = False Then
                Elemento.int_idunidad = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idunidad")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
                Elemento.str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("simbolo"))) = False Then
                Elemento.str_simbolo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("simbolo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("relacion1"))) = False Then
                Elemento.dbl_relacion1 = (obj_Reader.GetValue(obj_Reader.GetOrdinal("relacion1")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("relacion2"))) = False Then
                Elemento.dbl_relacion2 = (obj_Reader.GetValue(obj_Reader.GetOrdinal("relacion2")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("orden"))) = False Then
                Elemento.int_orden = (obj_Reader.GetValue(obj_Reader.GetOrdinal("orden")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("primario"))) = False Then
                Elemento.bit_primario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("primario")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idunidaddetalle = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idunidaddetalle")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idunidad"))) = False Then
            int_idunidad = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idunidad")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
            str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("simbolo"))) = False Then
            str_simbolo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("simbolo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("relacion1"))) = False Then
            dbl_relacion1 = (obj_Reader.GetValue(obj_Reader.GetOrdinal("relacion1")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("relacion2"))) = False Then
            dbl_relacion2 = (obj_Reader.GetValue(obj_Reader.GetOrdinal("relacion2")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("orden"))) = False Then
            int_orden = (obj_Reader.GetValue(obj_Reader.GetOrdinal("orden")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("primario"))) = False Then
            bit_primario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("primario")))
        End If
    End Sub

    Public Function Nuevo(Optional ByVal ObtenerID As Boolean = False) As Boolean
        Nuevo = False
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "unidaddetalle__Nuevo"
                .Parameters.Add("@idunidad", SqlDbType.Int).Value = int_idunidad
                .Parameters.Add("@nombre", SqlDbType.NVarChar).Value = str_nombre
                .Parameters.Add("@simbolo", SqlDbType.NVarChar).Value = str_simbolo
                .Parameters.Add("@relacion1", SqlDbType.Decimal).Value = dbl_relacion1
                .Parameters.Add("@relacion2", SqlDbType.Decimal).Value = dbl_relacion2
                .Parameters.Add("@orden", SqlDbType.Int).Value = int_orden
                .Parameters.Add("@primario", SqlDbType.Bit).Value = bit_primario
                .ExecuteNonQuery()
            End With
            Nuevo = True
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Crear el registro de unidaddetalle_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Function

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "unidaddetalle__Modificar"
                .Parameters.Add("@idunidaddetalle", SqlDbType.Int).Value = int_idunidaddetalle
                .Parameters.Add("@idunidad", SqlDbType.Int).Value = int_idunidad
                .Parameters.Add("@nombre", SqlDbType.NVarChar).Value = str_nombre
                .Parameters.Add("@simbolo", SqlDbType.NVarChar).Value = str_simbolo
                .Parameters.Add("@relacion1", SqlDbType.Decimal).Value = dbl_relacion1
                .Parameters.Add("@relacion2", SqlDbType.Decimal).Value = dbl_relacion2
                .Parameters.Add("@orden", SqlDbType.Int).Value = int_orden
                .Parameters.Add("@primario", SqlDbType.Bit).Value = bit_primario
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Modificar el registro de unidaddetalle_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idunidaddetalle As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "unidaddetalle__Eliminar"
                .Parameters.Add("@idunidaddetalle", SqlDbType.Int).Value = idunidaddetalle
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Eliminar el registro de unidaddetalle_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idunidaddetalle As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "unidaddetalle__Buscar"
                .Parameters.Add("@idunidaddetalle", SqlDbType.Int).Value = idunidaddetalle
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar el registro de unidaddetalle_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "unidaddetalle__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarUltimo registro de unidaddetalle_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "unidaddetalle__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarTodos los registros de unidaddetalle_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function CargarXidunidad(ByVal int_idunidad As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidunidad = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "unidaddetalle__CargarXidunidad"
                .Parameters.Add("@idunidad", SqlDbType.Int).Value = int_idunidad
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXidunidad = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarXidunidad los registros de unidaddetalle_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_unidaddetalle_Clon As New cls_unidaddetalle_
        obj_unidaddetalle_Clon.int_idunidaddetalle = Me.int_idunidaddetalle
        obj_unidaddetalle_Clon.int_idunidad = Me.int_idunidad
        obj_unidaddetalle_Clon.str_nombre = Me.str_nombre
        obj_unidaddetalle_Clon.str_simbolo = Me.str_simbolo
        obj_unidaddetalle_Clon.dbl_relacion1 = Me.dbl_relacion1
        obj_unidaddetalle_Clon.dbl_relacion2 = Me.dbl_relacion2
        obj_unidaddetalle_Clon.int_orden = Me.int_orden
        obj_unidaddetalle_Clon.bit_primario = Me.bit_primario
        obj_unidaddetalle_Clon.col_Items = Me.col_Items
        Return obj_unidaddetalle_Clon
    End Function

    Public Function CCantGuardar(ByVal prm_valor As Decimal) As Decimal
        Return Math.Round(((prm_valor / relacion2) * relacion1), int_cantdecimales)
    End Function

    Public Function CCantCargar(ByVal prm_valor As Decimal) As Decimal
        Return Math.Round(((prm_valor / relacion1) * relacion2), int_cantdecimales)
    End Function

    Public Function CValGuardar(ByVal prm_valor As Decimal) As Decimal
        Return Math.Round(((prm_valor / relacion1) * relacion2), int_cantdecimales)
    End Function

    Public Function CValCargar(ByVal prm_valor As Decimal) As Decimal
        Return Math.Round(((prm_valor / relacion2) * relacion1), int_cantdecimales)
    End Function

End Class
